package net.jueb.protobufs.client;

import net.jueb.protobufs.pojo.UsualMessageProtos;
import net.jueb.protobufs.pojo.UsualMessageProtos.Login.Builder;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;

public class ProtoClientHandler extends ChannelHandlerAdapter{
	
	@Override
	public void channelActive(ChannelHandlerContext ctx) throws Exception {
		Builder builder=UsualMessageProtos.Login.newBuilder();
		builder.setId(1);
		builder.setUsername("admin");
		builder.setPassword("123456");
		builder.setStatus(1);
		ctx.writeAndFlush(builder.build());
		System.out.println("发送登录信息完毕");
//		System.out.println("等待3秒……");
//		Thread.sleep(3000);
//		for(int i=0;i<2;i++)
//		{
//			net.jueb.protobufs.pojo.UsualMessageProtos.Logout.Builder builder2=UsualMessageProtos.Logout.newBuilder();
//			builder2.setId(2);
//			builder2.setStatus(2);
//			ctx.writeAndFlush(builder2.build());
//			System.out.println("发送登出信息完毕:"+i);
//		}
		
	}
	
	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg)throws Exception {
		if(msg==null)
		{
			return;
		}
		System.out.println("收到回复："+msg);
	}
	
	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)throws Exception {
		cause.printStackTrace();
		ctx.close();
	}
}
